<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Create extends Base_Controller {

    public $pageSize = 1;          //列表页的分页数据

    public function __construct() {
        parent::__construct();
        $this->load->model("article_model");
    }

    /**
     *  生成首页
     *  author:yuchong
     */
    public function create_index(){

        //todo 侧边栏标签, 后期可以根据标签点击量来展示一定标签
        $data['tag_arr']        = $this->get_tags();
        //首页推荐文章
        $data['commend_arr']    = $this->article_model->getIndexCommend();
        //第一屏 ----产业新闻---6篇文章
        $data['industry_arr']   = $this->article_model->getTypeCommend(5);
        //第二屏- -黑白评----三个好  三个坏
        $data['color_arr']      = $this->article_model->getTypeCommend(2);
        //第三屏 -- 周边实况
        $data['side_arr']       = $this->article_model->getTypeCommend(3);
        //第四屏 -- 5T视角
        $data['eye_arr']        = $this->article_model->getTypeCommend(4);
        //侧边栏 -- 焦点关注
        $data['focus_arr']      = $this->article_model->getTypeCommend(1);

        //类型
        $data['type_arr']       = $this->get_type_list();
        $html = $this->load->view("index", $data, true);
        $filename = FCPATH . 'html/index.html';
        write_file($filename, $html);
        echo "make index success!";
        exit;
    }



    /**
     *  生成栏目列表页
     *  author:yuchong
     */
    public function create_list(){
        set_time_limit(0);
        $typeid = $this->input->get("typeid", true) ? intval($this->input->get("typeid", true)) : 1;
        $page = max(intval($this->input->get('page', true)), 1);

        //左侧推荐文章
        $data['commend_arr'] = $this->article_model->getListCommend();
        //中间列表文章  todo 文章列表栏目 栏目id不能为0
        $list = $this->article_model->getList($typeid, $page, $this->pageSize);
        $data['list_arr']  = $list['result'];
        //文章的数量
        $page_count = ceil($list['total']/$this->pageSize);
        //分页代码
        $data['multi_url'] = frontmultiurl($page, $page_count, "http://www.tgame.com/index.php?c=create&m=create_list&page=page_mode");

        //侧边栏 -- 焦点关注
        $data['focus_arr']      = $this->article_model->getTypeCommend(1);
        //网站栏目
        $data['type_arr']       = $this->get_type_list();
        //todo 栏目名称不能固定
        $data['typename']       = $data['type_arr'][$typeid]['typename'];
        $html = $this->load->view("list", $data, true);

        //栏目文件夹
        $html_dir = FCPATH . 'html/' . $data['type_arr'][$typeid]['typedir']."/";
        create_dir($html_dir);
        //写入文件
        $filename = $html_dir . 'list_'.$page.'.html';
        write_file($filename, $html);

        $page == 1 && copy($html_dir . 'list_1.html', $html_dir.'index.html');
        $page++;
        if($page <= $page_count){
            echo 'creating the list...<script type="text/javascript">location="index.php?c=create&m=create_list&typeid='.$typeid.'&page='.$page.'";</script>';
        }else{
            echo 'create  list succesfully!';
            exit();
        }
    }



    /**
     *  生成某个栏目下的所有文章
     *  author:yuchong
     */
    public function create_all_cont(){
        set_time_limit(0);
        $typeid = $this->input->get("typeid", true) ? intval($this->input->get("typeid", true)) : 1;
        $page = max(intval($this->input->get('page', true)), 1);

        $id_arr = $this->article_model->getTypeIds($typeid);
        empty($id_arr) && show_error('该栏目不存在数据');
        $count  = sizeof($id_arr);
        $index = intval($page-1);

        $this->create_single_cont($id_arr[$index]['id']);
        $page++;
        if($page <= $count)
        {
            echo 'creating the article....!<script type="text/javascript">location="/index.php?c=create&m=create_all_cont&typeid='.$typeid.'&page='.$page.'";</script>';
        }
        else
        {
            echo 'create all_cont successfully!';
            exit();
        }
    }


    /**
     * 生成文章页
     * author:yuchong
     */
    private function create_single_cont($id){
        //todo   正式测试的时候id为动态数据
        //$id = $this->input->get("id", true) ? intval($this->input->get("id", true)) : 1;
        //文章基本信息
        $data['data']   = $this->article_model->getOneArticle($id);
        $typeid = $data['data']['typeid'];
        //编辑基本信息
        $data['editor'] = $this->get_editor_info($data['data']['writer']);
        //相关文章的推荐
        $data['commend'] = $this->article_model->getContCommend($id, $data['data']['typeid']);

        //侧边栏 -- 焦点关注
        $data['focus_arr']      = $this->article_model->getTypeCommend(1);
        //网站栏目
        $data['type_arr']       = $this->get_type_list();
        //todo 栏目名称不能固定
        $data['typename']       = $data['type_arr'][$typeid]['typename'];
        $data['typeurl']        = $data['type_arr'][$typeid]['typedir'];
        $html = $this->load->view("cont", $data, true);

        //文章地址
        $html_dir = FCPATH .'html/' . $data['typeurl'] . '/';
        create_dir($html_dir);
        write_file($html_dir . $id . ".html", $html);
        return true;
    }



    /**
     *  单个生成
     * author:yuchong
     */
    public function create_cont(){
        //todo   正式测试的时候id为动态数据
        $id = $this->input->get("id", true) ? intval($this->input->get("id", true)) : 1;
        //文章基本信息
        $data['data']   = $this->article_model->getOneArticle($id);
        $typeid = $data['data']['typeid'];
        //编辑基本信息
        $data['editor'] = $this->get_editor_info($data['data']['writer']);
        //相关文章的推荐
        $data['commend'] = $this->article_model->getContCommend($id, $data['data']['typeid']);

        //侧边栏 -- 焦点关注
        $data['focus_arr']      = $this->article_model->getTypeCommend(1);
        //网站栏目
        $data['type_arr']       = $this->get_type_list();
        //todo 栏目名称不能固定
        $data['typename']       = $data['type_arr'][$typeid]['typename'];
        $data['typeurl']        = $data['type_arr'][$typeid]['typedir'];
        $html = $this->load->view("cont", $data, true);

        //文章地址
        $html_dir = FCPATH .'html/' . $data['typeurl'] . '/';
        create_dir($html_dir);
        write_file($html_dir . $id . ".html", $html);
        echo "make cont success!";
        exit;
    }



    /**
    *   获取标签
     */
    private function get_tags(){
        $this->load->model("tag_model");
        $res = $this->tag_model->getlist();
        return $res;
    }


    /**
    *   栏目获取
     */
    private function get_type_list(){
        $this->load->model("type_model");
        $res = $this->type_model->getList();
        if($res){
            return array_change_key($res, "catid");
        }
        return false;
    }

    /**
    *   获取编辑信息
     */
    public function get_editor_info($writer){
        $this->load->model("editor_model");
        return $this->editor_model->getByNickname($writer);
    }




}